package petshop.agronunes.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import petshop.agronunes.entidade.Funcionario;;


public class FuncionarioDAO {
	private Session session;
	
	public FuncionarioDAO() {
		this.session = HibernateUtil.getSession();
	}
	
	public Integer insert(Funcionario funcionario) throws Exception {
		try {
			Integer id = (Integer) session.save(funcionario);
			session.beginTransaction().commit();
			return id;
		} catch(Exception e) {
			throw new Exception(e);
		} finally {
			session.close();
		}
	}
	
	public void update(Funcionario funcionario) throws Exception {
		try {
			session.update(funcionario);
			session.beginTransaction().commit();
		} catch(Exception e) {
			throw new Exception(e);
		} finally {
			session.close();
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Funcionario> getAll() throws Exception {
		try {
			Criteria query = session.createCriteria(Funcionario.class);
			return query.list();
		} catch(Exception e) {
			throw new Exception(e);
		} finally {
			session.close();
		}
	}
	
	public Funcionario GetById(Integer id) throws Exception {
		try {
			Criteria query = session.createCriteria(Funcionario.class);
			query.add(Restrictions.idEq(id));
			return (Funcionario) query.uniqueResult();
		} catch(Exception e) {
			throw new Exception(e);
		} finally {
			session.close();
		}
	}
	
	public void delete(Funcionario funcionario) throws Exception {
		try {
			session.delete(funcionario);
			session.beginTransaction().commit();
		} catch(Exception e) {
			throw new Exception(e);
		} finally {
			session.close();
		}
	}
}